import { Injectable } from '@angular/core';
import { UserInfoModel } from '../../domain/user-info-model';
import { Observable, Subject } from 'rxjs';
import * as CryptoJS from 'crypto-js';

@Injectable()
export class UserAuthService {
  public userKey = 'currentUser';
  public storage: Storage = sessionStorage;
  public localStorage: Storage = localStorage;
  public userSubject = new Subject();
  public Login: boolean = true;

  constructor() {
  }

  /**
   * 存储用户信息
   */
  public storeUserInfo(userInfo: string) {
    this.storage.setItem(this.userKey, userInfo);
    const user = JSON.parse(userInfo)['userAccount'];
    this.userSubject.next(user);
  }

  public storeUserInfoLocal(userInfo: string) {
    this.localStorage.setItem(this.userKey, userInfo);
    const user = JSON.parse(userInfo)['userAccount'];
    this.userSubject.next(user);
  }

  public storUserPostLocal(postKey: string, userInfo: string) {
    this.localStorage.setItem(postKey, userInfo);
    // const user = JSON.parse(userInfo)['userAccount'];
    // this.userSubject.next(user);
  }

  /**
   * 移除用户信息
   */
  public removeUserInfo() {
    this.storage.removeItem(this.userKey);
    this.storage.removeItem('userCompany');
    this.localStorage.removeItem(this.userKey);
    this.userSubject.next(null);
  }

  /**
   * 获取存储在 storage 中的用户信息
   * @return {(UserInfoModel | null)} userInfoObj||null
   */
  public getUserInfoObj(): UserInfoModel {
    try {
      let userInfo = this.storage.getItem(this.userKey);
      if(userInfo === null) return null;
      const userInfoObj = JSON.parse(userInfo);
      return !!userInfoObj ? userInfoObj : null;
    } catch (e) {
      return null;
    }
  }

  /**
   *获取存储在 localStorage 中的用户信息
   * @returns {UserInfoModel}
   */
  public getUserInfoLocalObj(): UserInfoModel {
    try {
      const userInfo = this.localStorage.getItem(this.userKey);
      if(userInfo === null) return null;
      const userInfoObj = JSON.parse(userInfo);
      return !!userInfoObj ? userInfoObj : null;
    } catch (e) {
      return null;
    }
  }

  public getUser(): Observable<any> {
    let that = this;
    return Observable.create(function(observer) {
      let um = that.getUserInfoObj();
      if (um) {
        observer.next(um['userAccount']);
      } else {
        observer.next(null);
      }
    });
  }

  /**
   * 判断是否处于登录状态
   */
  public isLogin() {
    if (!!!this.storage.getItem(this.userKey)) {
      let userInfo = this.localStorage.getItem(this.userKey);
      if (!!userInfo) {
        this.storeUserInfo(userInfo);
      }
    }
    return !!this.storage.getItem(this.userKey);
  }

  /**
   * 获取当前用户token
   * @return token - token
   */
  public getToken() {
    return !!this.getUserInfoObj() ? this.getUserInfoObj().token : null;
  }

  /**
   * 获取当前用户terminal
   * @return terminal - terminal
   */
  public getTerminal() {
    return !!this.getUserInfoObj() ? this.getUserInfoObj().terminal : null;
  }

  /**
   * 获取当前用户post
   * @return terminal - post
   */
  public getPost(postKey: string): UserInfoModel {
    try {
      const post = this.localStorage.getItem(postKey);
      const userInfoObj = JSON.parse(post);
      return !!userInfoObj ? userInfoObj : null;
    } catch (e) {
      return null;
    }
  }

  public getUserHash(userId: string, isCreated: boolean = true): string {
    let hashKey = 'hash_' + userId;
    let hash = this.localStorage.getItem(hashKey);
    if (hash === null && isCreated) {
      const timestamp = new Date().getTime();
      hash = CryptoJS.MD5(userId + '_' + timestamp).toString();
      this.localStorage.setItem(hashKey, hash);
    }
    return hash;
  }
}
